import type { ObjectDirective } from 'vue'

export const digitNumber: ObjectDirective = {
  mounted(el: HTMLButtonElement, binding, vnode) {
    const trigger = (el, type) => {
      const e = document.createEvent('HTMLEvents')
      e.initEvent(type, true, true)
      el.dispatchEvent(e)
    }
    el.addEventListener('keyup', function (e) {
      const input = e.target as any
      const value = input.value
      if (value === '-') {
        input.value = null
      } else if (value === 0) {
        input.value = 0
      } else if (value === '') {
        input.value = ''
      } else {
        input.value = isNaN(Math.abs(parseInt(value))) ? 0 : Math.abs(parseInt(value))
      }
      trigger(input, 'input')
    })
  }
  // // console.log(vnode.ref.i.ctx.hasPermission(action), '虚拟')
  // if (!hasPermission(action)) {
  //   if (effect == 'disabled') {
  //     el.disabled = true
  //     el.setAttribute('title', '没有权限')
  //   } else {
  //     el.remove()
  //   }
  //   console.log(action, effect)
  // }
}
